package com.bkitmobile.kesu.android.GUI;

import com.bkitmobile.kesu.android.ClassInfo.PlaceInfo;
import com.bkitmobile.kesu.android.connectServer.staticClass;
import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MapAddLocation extends Dialog implements android.view.View.OnClickListener{
	private Context context;
	private Spinner type;
	private EditText edName, edAddress, edDescription;
	private Button btnAdd, btnCancel;
	private GeoPoint point;
	private KesuMap main;
	private boolean isAdd;
	private PlaceInfo pi;
	private String []types;
	private CheckBox checkIsPersonal;
	public MapAddLocation(Context context) {
		super(context);
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_add_location);
		init();
	}
	/**
	 * init GUI's atributes
	 */
	private void init() {
		type = (Spinner) findViewById(R.id.map_add_type);
		edName = (EditText) findViewById(R.id.map_add_name);
		edAddress = (EditText) findViewById(R.id.map_add_address);
		edDescription = (EditText) findViewById(R.id.map_add_describe);
		btnAdd = (Button) findViewById(R.id.map_add_btnAdd);
		btnCancel = (Button) findViewById(R.id.map_add_btnCancel);
		checkIsPersonal = (CheckBox) findViewById(R.id.check_personal);
		Log.d("Check Null", "" + edName);
		types = ((Activity)context).getResources().getStringArray(R.array.map_items_public);
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, R.layout.item, types);
		type.setAdapter(arrayAdapter);
		btnAdd.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}
	/**
	 * Show the dialog when add new place
	 * @param point: the point result when tap on the map
	 * @param main: the instant of KesuMap activity
	 */
	public void show(GeoPoint point,KesuMap main) {
		super.show();
		
		this.point = point;
		this.main = main;
		type.setEnabled(true);
		edName.setText("");
		edAddress.setText("");
		edDescription.setText("");
		isAdd = true;
		btnAdd.setText(main.getString(R.string.map_add_add));
	}
	/**
	 * Show the dialog when edit an place
	 * @param pi
	 * @param main
	 */
	public void show(PlaceInfo pi, KesuMap main){
		super.show();
		this.main = main;
		this.pi = pi;
		Log.d("Show", "PlaceInfo");
		type.setEnabled(false);
		
		String strType = pi.getType();
		for(int i = 0; i<types.length; i++){
			if(strType.equals(types[i])){
				type.setSelection(i);
				break;
			}
		}
		edName.setText(pi.getName());
		edAddress.setText(pi.getAddress());
		edDescription.setText(pi.getInfo());
		isAdd = false;
		btnAdd.setText(main.getString(R.string.map_add_save));
	}
	
	@Override
	public void onClick(View v) {
		if(v == btnAdd){
			String name, address, description,type;
			description  = edDescription.getText().toString();
			address = edAddress.getText().toString();
			if(address.equals(""))
				return;
			if(description.equals(""))
				return;
			name = edName.getText().toString();
			
			if(isAdd){
				type = this.type.getSelectedItem().toString();
				int personal = checkIsPersonal.isChecked() ? 0 : 1;
				String request = "<newloc><name>"+name+"<addr>"+address+"<type>"
								+type+"<descr>"+description
								+"<lng>"+point.getLongitudeE6()/1E6+"<lat>"+point.getLatitudeE6()/1E6
								+"<privacy>"+personal;
				staticClass.socket.println(request);
				Log.d("request", request);
				//TODO: add new overlay of type with description
				main.addNewPlace(type, name, description, address, point);
			}
			else{
				pi.setName(name);
				pi.setAddress(address);
				pi.setInfo(description);
				//<editLoc><lat>latE6<lng>lngE6<name>name<addr>addr<info>info
				String request = "<editLoc><lat>"+pi.getLatE6()+"<lng>"+pi.getLngE6()
								+"<name>"+name+"<addr>"+address+"<info>"+description;
				staticClass.socket.println(request);
			}
		}
		main.cancel();
		if(isShowing())
			dismiss();
	}
	
	
}
//de dung sau nay neu co thay doi gi
class ItemsAdapter<String> extends ArrayAdapter<String>{
	Context context;
	String []items;
	public ItemsAdapter(Context context, String[] objects) {
		super(context, R.layout.map_add_location_item, objects);
		this.context = context;
		this.items = objects;
	}
	public View getView(int position,  View convertView, ViewGroup parent) {
		View item = convertView;
		if(item == null){
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			item = inflater.inflate(R.layout.map_add_location_item, null);
		}
		TextView text = (TextView) item.findViewById(R.id.map_add_location_item_text);
		ImageView icon = (ImageView) item.findViewById(R.id.map_add_location_item_icon);
		text.setText((CharSequence) items[position]);
		
		
		return item;
	}
	private String solveName(String str){
		String result = (String) ("m_"+ str.toString().toLowerCase());
		result.toString().replaceAll(" ", "_");
		return result;
	}
}
